package com.ruoyi.web.controller.substation;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.system.domain.SArea;
import com.ruoyi.system.domain.SSubstation;
import com.ruoyi.system.service.ISSubstationService;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.SSubstationType;
import com.ruoyi.system.service.ISSubstationTypeService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 变电站类型Controller
 * 
 * @author ruoyi
 * @date 2023-07-05
 */
@Api(tags = "变电站类型管理")
@RestController
@RequestMapping("/substation/type")
public class SSubstationTypeController extends BaseController
{
    @Autowired
    private ISSubstationTypeService sSubstationTypeService;
    @Autowired
    private ISSubstationService sSubstationService;

    /**
     * 查询变电站类型列表
     */
    @ApiOperation("获取变电站类型列表")
    @PreAuthorize("@ss.hasPermi('substation:type:list')")
    @GetMapping("/list")
    public TableDataInfo list(SSubstationType sSubstationType)
    {
        startPage();
        List<SSubstationType> list = sSubstationTypeService.list(sSubstationType.queryWrapper());
        return getDataTable(list);
    }

    /**
     * 导出变电站类型列表
     */
    @ApiOperation("导出变电站类型列表")
    @PreAuthorize("@ss.hasPermi('substation:type:export')")
    @Log(title = "变电站类型", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SSubstationType sSubstationType)
    {
        List<SSubstationType> list = sSubstationTypeService.list(sSubstationType.queryWrapper());
        ExcelUtil<SSubstationType> util = new ExcelUtil<SSubstationType>(SSubstationType.class);
        util.exportExcel(response, list, "变电站类型数据");
    }

    /**
     * 获取变电站类型详细信息
     */
    @ApiOperation("获取变电站类型详细")
    @ApiImplicitParam(name = "id", value = "变电站类型ID", required = true, dataType = "String", paramType = "path")
    @PreAuthorize("@ss.hasPermi('substation:type:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return success(sSubstationTypeService.getById(id));
    }

    /**
     * 新增变电站类型
     */
    @ApiOperation("新增变电站类型")
    @PreAuthorize("@ss.hasPermi('substation:type:add')")
    @Log(title = "变电站类型", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SSubstationType sSubstationType)
    {
        long count = sSubstationTypeService.count(new LambdaQueryWrapper<SSubstationType>()
                .eq(SSubstationType::getType, sSubstationType.getType()));
        if(count > 0){
            throw new ServiceException("类型：" + sSubstationType.getType() + " 已存在");
        }
        return toAjax(sSubstationTypeService.save(sSubstationType));
    }

    /**
     * 修改变电站类型
     */
    @ApiOperation("更新变电站类型")
    @PreAuthorize("@ss.hasPermi('substation:type:edit')")
    @Log(title = "变电站类型", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SSubstationType sSubstationType)
    {
        SSubstationType byId = sSubstationTypeService.getById(sSubstationType.getId());
        if(!byId.getType().equals(sSubstationType.getType())) {
            long count = sSubstationTypeService.count(new LambdaQueryWrapper<SSubstationType>()
                    .eq(SSubstationType::getType, sSubstationType.getType()));
            if(count > 0){
                throw new ServiceException("类型：" + sSubstationType.getType() + " 已存在");
            }
        }
        return toAjax(sSubstationTypeService.updateById(sSubstationType));
    }

    /**
     * 删除变电站类型
     */
    @ApiOperation("删除变电站类型")
    @ApiImplicitParam(name = "ids", value = "变电站类型ID集合", required = true, dataType = "String", paramType = "path")
    @PreAuthorize("@ss.hasPermi('substation:type:remove')")
    @Log(title = "变电站类型", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable List<String> ids)
    {
        for (String id : ids) {
            long count = sSubstationService.count(new LambdaQueryWrapper<SSubstation>()
                    .eq(SSubstation::getTypeId, id));
            if(count > 0){
                throw new ServiceException("类型已绑定变电站，请先解除绑定！");
            }
        }

        return toAjax(sSubstationTypeService.removeByIds(ids));
    }
}
